﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Lu.Net;
using System.IO.Ports;
using System.Windows.Forms;
using Lu.Net;

namespace LuxJQR
{
    class SerialCom
    {

        public bool bIsConnect = false;
        public SerialPortManager SerialPort = null;

        public string CloseCom()
        {
            try
            {
                SerialPort.Close();
                return "";
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public string ConnectCom(string serialName, int BaudRate, int DateBits, Parity parity, StopBits sb)
        {
            try
            {
                SerialPort = new SerialPortManager();


                if (!SerialPort.IsOpen)
                {
                    SerialPort.ReadBufferSize = 4096;
                    SerialPort.WriteBufferSize = 4096;

                    // SerialPort.ReceiveData += MainForm.serialPort2_ReceiveData;
                    SerialPort.SetComPortParam(serialName, BaudRate, DateBits, parity, sb);
                    if (SerialPort.IsOpen == true)//如果打开状态，则先关闭一下
                    {
                        SerialPort.Close();
                    }
                    SerialPort.Open();
                }
                else
                {
                    SerialPort.Close();//关闭串口
                    SerialPort = null;
                }

                bIsConnect = true;
                return "";
            }
            catch (Exception ex)
            {
                bIsConnect = false;
                return "连接失败：" + ex.Message;
            }

        }
        public void SendData(string msg)
        {
            SerialPort.SendData(msg);
        }


        public static void serialPort_ReceiveData(byte[] data)
        {
            string strRecvData = null;
            strRecvData = Encoding.UTF8.GetString(data);
            //  ColoredOutput.WriteLine("串口设置接收信息=" + recvData);
            

            if (ListData.Count>1)
            {
                //Param.bBaseCamTriger = true;
                //ColoredOutput.WriteLine("收到(底座)机械手发送的底座拍照信号" + strRecvData);
                //GObj.SDKrobotcom.SendData(strRecvData);
                return;
            }

        }

        #region  串口接收函数解析

        public static List<string> ListData = new List<string>();
        public  void AnalysisData(string strData, ref List<string> LstData)
        {
            try
            {
                LstData.Clear();
                string strTmp = strData;
                string[] strComData = strTmp.Split(',');
                int num = strComData.Length;
                for (int i = 0; i < num; i++)
                {
                    LstData.Add(strComData[i]);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        #endregion






    }
}
